package com.ruoyi.mobile.project.sms.service;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.ruoyi.db.sms.domain.SmsCoupon;
import com.ruoyi.db.sms.domain.SmsCouponUser;
import com.ruoyi.db.sms.domain.query.SmsCouponQueryVo;
import com.ruoyi.db.sms.domain.query.SmsCouponUserQueryVo;
import com.ruoyi.db.sms.mapper.SmsCouponMapper;
import com.ruoyi.db.sms.mapper.SmsCouponUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class WxSmsCouponService
{
    @Autowired
    private SmsCouponMapper couponMapper;

    @Autowired
    private SmsCouponUserMapper couponUserMapper;

    /**
     * 查询，空参数
     *
     * @param offset
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    //蔡江恩
    public List<SmsCoupon> queryList(int offset, int limit, String sort, String order)
    {
        return couponMapper.selectSmsCouponList(new SmsCouponQueryVo());
    }


    public List<SmsCoupon> queryAvailableList(Long userId, int offset, int limit)
    {
        PageHelper.startPage(offset, limit);
        return couponMapper.selectAvailableSmsCouponList(userId);


    }

    public List<SmsCoupon> queryList(int offset, int limit)
    {
        PageHelper.startPage(offset, limit);
        return couponMapper.selectSmsCouponList(new SmsCouponQueryVo());
    }
//蔡江恩
    public SmsCoupon findById(Long id)
    {


            return couponMapper.selectSmsCouponById(id);
    }
//蔡江恩
    public SmsCoupon findByCode(String code)
    {
        SmsCouponQueryVo smsCouponQueryVo = new SmsCouponQueryVo();
        List<SmsCoupon> list1 = new ArrayList<>();

            int z = couponMapper.selectSmsCouponList(smsCouponQueryVo).size();
            for (int i = 0; i < z; i++) {

                String a = couponMapper.selectSmsCouponList(smsCouponQueryVo).get(i).getCode();

                if (a==null)
                    list1 = list1;
                else if(code.equals(a)) {
                    list1.add(couponMapper.selectSmsCouponList(smsCouponQueryVo).get(i));
                }
                else
                    list1=list1;
            }

            if (list1.size() > 1) {
                throw new RuntimeException("");
            } else if (list1.size() == 0) {
                return null;
            } else {
                return list1.get(0);
            }

    }

    /**
     * 查询新用户注册优惠券
     *
     * @return
     */
    public List<SmsCoupon> queryRegister()
    {
        SmsCouponQueryVo smsCouponQueryVo = new SmsCouponQueryVo();
        List<SmsCoupon> list1 = new ArrayList<>();

        int z = couponMapper.selectSmsCouponList(smsCouponQueryVo).size();
        for (int i = 0; i < z; i++) {

            Integer a = couponMapper.selectSmsCouponList(smsCouponQueryVo).get(i).getGivenType();

            if (a==1)
                list1.add(couponMapper.selectSmsCouponList(smsCouponQueryVo).get(i));

            else
                list1=list1;
        }
        return list1;
    }

    public List<SmsCoupon> querySelective(String name, Short type, Short status, Integer page, Integer limit, String sort, String order)
    {
        return null;
    }

    public void add(SmsCoupon coupon)
    {

    }

    public int updateById(SmsCoupon coupon)
    {
        return 0;
    }

    public void deleteById(Integer id)
    {

    }

    private String getRandomNum(Integer num)
    {
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        base += "0123456789";

        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < num; i++)
        {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 生成优惠码
     *
     * @return 可使用优惠码
     */
    public String generateCode()
    {
        String code = getRandomNum(8);
        while (findByCode(code) != null)
        {
            code = getRandomNum(8);
        }
        return code;
    }

    /**
     * 查询过期的优惠券:
     * 注意：如果timeType=0, 即基于领取时间有效期的优惠券，则优惠券不会过期
     *
     * @return
     */
    public List<SmsCoupon> queryExpired()
    {
        return null;
    }
}
